package com.D5Power.Objects.Effects
{
	import com.D5Power.Objects.GameObject;
	import com.D5Power.basic.GraphicsResource;
	import com.D5Power.display.D5TextField;
	import com.D5Power.scene.BaseScene;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	
	/**
	 * 伤害（丢血）特效
	 */ 
	public class BasicHurtEffect extends EffectObject
	{
		protected var _target:Point = new Point();
		public var flyY:uint = 50;
		
		/**
		 * @param	scene		主程序场景
		 * @param	hurt		伤害值
		 * @param	color		显示颜色
		 * @param	canMiss		是否显示MISS
		 */ 
		public function BasicHurtEffect(scene:BaseScene,hurt:uint,color:uint=0xff0000,canMiss:Boolean=true)
		{
			super(scene);
			buildBuffer(hurt,color,canMiss);
			_zOrderF = 200;
		}
		
		override public function setPos(px:uint,py:uint):void
		{
			super.setPos(px,py);
			
			_target.x = px;
			_target.y = py-flyY;			
			
		}
		
		protected function buildBuffer(hurt:uint,color:uint,canMiss:Boolean,font:String='',fontSize:uint = 12):void
		{
			var textFiled:D5TextField;
			if(canMiss)
			{ 
				textFiled = new D5TextField(hurt==0 ? 'Miss' : hurt.toString(),color,-1,-1,fontSize) 
			}else{
				textFiled = new D5TextField(hurt.toString(),color,-1,-1,fontSize) 
			}
			textFiled.autoGrow();
			if(font!='')
			{
				textFiled.embedFonts=true;
				textFiled.textFormat.font = font;
				//textFiled.flush();
			}else{
				textFiled.fontBorder = 0;
			}
			
			
			_buffer = new BitmapData(textFiled.width,textFiled.height,true,0x00000000);
			_buffer.draw(textFiled);
			
			graphics = new GraphicsResource(new Bitmap(_buffer));
			
			textFiled.clear();
			textFiled=null;
		}
		
		override protected function run():void
		{
			Pos.y += (_target.y-Pos.y)/15;
			if(Point.distance(Pos,_target)<5)
			{
				_scene.removeObject(this);
				return;
			}
		}
		
		/**
		 * 渲染自己
		 */ 
		override public function renderMe():void
		{
			if(_buffer==null || _target==null) return;
			super.renderMe();
		}
		/**
		 * 清除
		 */ 
		override public function clear():void
		{
			_buffer.dispose();
			_buffer=null;
		}
	}
}